prefix := /usr/local
bin-prefix := $(prefix)/bin
programs := ++b --b
installed-programs := $(addprefix $(bin-prefix)/,$(programs))
build-type := release
release-flags := -Os
debug-flags := -g -Wall -Wextra -Weffc++ -Werror 
sources := $(wildcard *.cpp)

.PHONY: all
all: $(programs)

++b: lcd_t.o brighter.o
	g++ $(filter-out GNUmakefile,$^) -o $@

--b: lcd_t.o dimmer.o
	g++ $(filter-out GNUmakefile,$^) -o $@

$(foreach s,$(sources), \
  $(eval \
    $(strip \
      $(s:cpp=o): $(s) GNUmakefile; g++ -c $($(build-type)-flags) -o $$@ $$< \
     ) \
   ) \
 )

# run this as root
.PHONY: install
install: $(installed-programs)

$(foreach p,$(programs), \
  $(eval \
    $(strip \
      $(bin-prefix)/$(p): $(p); install -s -m 4755 -o root -- $$< $$@ \
     ) \
   ) \
 )

# run this as root
.PHONY: uninstall
uninstall:
	rm -f -- $(installed-programs)

.PHONY: clean
clean:
	rm -f *.o -- $(programs)
